package com.bdtop.dbl.ui.view;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bdtop.dbl.R;
import com.bdtop.dbl.utils.DBLDebug;

public class TipsView extends FrameLayout {
	public TipsView(Context context) {
		this(context, null);
	}

	public TipsView(Context context, AttributeSet attrs) {
		super(context, attrs);

		LayoutInflater.from(context).inflate(R.layout.view_tips, this);
		txInfo = (TextView) findViewById(R.id.tx_info);

		value = getResources().getString(R.string.loc_tips);
	}

	@Override protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		stopAnim();
		DBLDebug.logd("tips view DetachedFromWindow");
	}

	private String value;
	private TextView txInfo;
	private int points;
	private String[] p = { ".", "..", "..." };

	public void startAnim() {
		setVisibility(View.VISIBLE);
		handler.removeCallbacks(runAnim);
		handler.post(runAnim);

		handler.removeCallbacks(runTimeout);
		handler.postDelayed(runTimeout, 30 * 1000);
	}

	public void stopAnim() {
		handler.removeCallbacks(runAnim);
		setVisibility(View.GONE);

		handler.removeCallbacks(runTimeout);
	}

	private Handler handler = new Handler();
	private Runnable runAnim = new Runnable() {
		public void run() {
			points++;
			if (points > 2) {
				points = 0;
			}

			String result = value + p[points];
			txInfo.setText(result);

			handler.postDelayed(runAnim, 1000);
		}
	};

	private Runnable runTimeout = new Runnable() {
		public void run() {
			if (TipsView.this != null) {
				TipsView.this.setVisibility(View.GONE);
			}
		}
	};
}
